﻿using System;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using AnyOffice.Components;
using AnyOffice.DAL;
using AnyOffice.Model;

namespace AnyOffice.general.rms.roll_room
{
    public partial class edit : ModulePage
    {
        protected string strTemp;

        private void BindDept(ref DropDownList ddlParentDept, int DeptId, string strTemp)
        {
            DataTable table = new DepartmentDao().SelectChildAll(base.Conn, DeptId);
            if (table.Rows.Count > 0)
            {
                strTemp = strTemp + "|　";
                for (int i = 0; i < table.Rows.Count; i++)
                {
                    ListItem item = new ListItem(strTemp + "|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                    ddlParentDept.Items.Add(item);
                    this.BindDept(ref ddlParentDept, int.Parse(table.Rows[i]["DeptId"].ToString()), strTemp);
                }
            }
        } 

 

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.Page.IsPostBack)
            {
                int num = (Request.QueryString["page"] != null) ? int.Parse(Request.QueryString["page"].ToString()) : 1;
                this.ViewState["CurrentPage"] = num;
                int roomId = (Request.QueryString["room_id"] != null) ? int.Parse(Request.QueryString["room_id"].ToString()) : 0;
                this.ViewState["RoomId"] = roomId;
                ListItem item = new ListItem("", "");
                this.ddlDeptId.Items.Add(item);
                DataTable table = new DepartmentDao().SelectParentAll(base.Conn);
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        item = new ListItem("|─" + table.Rows[i]["DeptName"].ToString(), table.Rows[i]["DeptId"].ToString());
                        this.ddlDeptId.Items.Add(item);
                        this.strTemp = "";
                        this.BindDept(ref this.ddlDeptId, int.Parse(table.Rows[i]["DeptId"].ToString()), this.strTemp);
                    }
                }
                DataTable table2 = new RmsRoomDao().SelectByRoomId(base.Conn, roomId);
                if (table2.Rows.Count > 0)
                {
                    this.txtRoomCode.Text = table2.Rows[0]["RoomCode"].ToString();
                    this.txtRoomName.Text = table2.Rows[0]["RoomName"].ToString();
                    if ((table2.Rows[0]["DeptId"] != DBNull.Value) && (this.ddlDeptId.Items.FindByValue(table2.Rows[0]["DeptId"].ToString()) != null))
                    {
                        this.ddlDeptId.Items.FindByValue(table2.Rows[0]["DeptId"].ToString()).Selected = true;
                    }
                    if (table2.Rows[0]["Remark"] != DBNull.Value)
                    {
                        this.txtRemark.Text = table2.Rows[0]["Remark"].ToString();
                    }
                }
                else
                {
                    this.pnlMain.Visible = false;
                    this.litMessage.Text = base.ShowPrompt("无此卷库", "index1.aspx");
                }
            }
            if (this.Page.IsPostBack)
            {
                RmsRoomData data = new RmsRoomData();
                data.RoomId = Convert.ToInt32(this.ViewState["RoomId"].ToString());
                data.RoomCode = this.txtRoomCode.Text;
                data.RoomName = this.txtRoomName.Text;
                if (this.ddlDeptId.SelectedValue != string.Empty)
                {
                    data.DeptId = Convert.ToInt32(this.ddlDeptId.SelectedValue);
                }
                if (this.txtRemark.Text != string.Empty)
                {
                    data.Remark = this.txtRemark.Text;
                }
                data.ModUser = base.User.Identity.Name;
                data.ModTime = DateTime.Now;
                new RmsRoomDao().Update(base.Conn, data);
                this.pnlMain.Visible = false;
                this.litMessage.Text = base.ShowPrompt("卷库修改成功！", "index1.aspx?page=" + this.ViewState["CurrentPage"].ToString());
            }


        }
    }
}
